package timeline.util.submit;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

import timeline.util.TimeLineClient;
import timeline.util.model.TaskSubmitModel;
import timeline.util.model.TimeLineConnectModel;
import timeline.util.worker.TimeLineWorker;

public class TaskSubmit {
	private final static Logger logger = Logger.getLogger(TimeLineClient.class
			.getName());
	
	public static void main(String[] args) {
		TaskSubmit taskSubmit = new TaskSubmit();
		TaskSubmitModel model = new TaskSubmitModel();
		model.serverName = args[2];
		model.runningTarget = args[3];
		model.filePath = args[4];
		taskSubmit.submitTask(args[0],Integer.parseInt(args[1]),model);
	}
	
	public  void submitTask(String masterIP,int MasterPort,TaskSubmitModel model) {
		Socket socket = null;
		ObjectOutputStream os = null;
		ObjectInputStream is = null;

		try {
			socket = new Socket(masterIP, MasterPort);
			os = new ObjectOutputStream(socket.getOutputStream());
			os.writeObject(model);
			os.flush();

			is = new ObjectInputStream(new BufferedInputStream(
					socket.getInputStream()));
			Object obj = is.readObject();
			System.out.println(obj+"");
		} catch (IOException ex) {
			logger.log(Level.SEVERE, null, ex);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (Exception ex) {
			}
			try {
				os.close();
			} catch (Exception ex) {
			}
			try {
				socket.close();
			} catch (Exception ex) {
			}
		}
	}
}
